package com.huaqin.punan.anim;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private View view;
	private Button mButton;
	
	private Animation myAnimation;
	private Animation myAnimation_Translate;
	
	private int durationMillis = 1000;
	private int delayMillis = 10;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		view = (ImageView)findViewById(R.id.myimage);
		
		myAnimation = AnimationUtils.loadAnimation(this, R.anim.ani_in);
		
		myAnimation_Translate=new TranslateAnimation(0.0f, 100.0f, 0.0f, 0.0f); 
		
		
		mButton = (Button)findViewById(R.id.mybtn);
		mButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				//mImageView.startAnimation(myAnimation);
				//mImageView.startAnimation(myAnimation_Translate);
				
				slideview(0,100);
				
			}});
		
		
		view.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_LONG).show();
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	public void slideview(final float p1, final float p2) {
	    TranslateAnimation animation = new TranslateAnimation(p1, p2, 0, 0);
	    animation.setInterpolator(new OvershootInterpolator());
	    animation.setDuration(durationMillis);
	    animation.setStartOffset(delayMillis);
	    animation.setAnimationListener(new Animation.AnimationListener() {
	        @Override
	        public void onAnimationStart(Animation animation) {
	        }
	        
	        @Override
	        public void onAnimationRepeat(Animation animation) {
	        }
	        
	        @Override
	        public void onAnimationEnd(Animation animation) {
	            int left = view.getLeft()+(int)(p2-p1);
	            int top = view.getTop();
	            int width = view.getWidth();
	            int height = view.getHeight();
	            view.clearAnimation();
	            view.layout(left, top, left+width, top+height);
	        }
	    });
	    view.startAnimation(animation);
	}

}
